import axios from 'axios'
import {Toast} from 'vant';
const baseUrl='http://223.4.72.61:9888/dingdang/app'
 export default  class Http{
  /**
 * 封装get方法
 * @param url
 * @param params
 * @returns {Promise}
 */

 get(url,params={},loading={isShow:false,params:{
        duration:0,
        forbidClick:true,
        message:"加载中...",
        loadingType:"spinner"
   }}){
    url=baseUrl+url
   if(loading&&loading.isShow){
      Toast.loading(loading.params);
   }  
   
  return new Promise((resolve,reject) => {
    axios.get(url,{
       params:params,
       headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
         "token":localStorage.getItem("token")
       }
      })
      .then(response => {
        if(loading&&loading.isShow){
            Toast.clear()
         }
         
        if(response.data.code === 200){
        //返回成功处理  这里传的啥 后续调用的时候 res就是啥
          resolve(response.data);//我们后台所有数据都是放在返回的data里所以这里统一处理了
        }else{
           reject(response.message);
         }
      })
      .catch(err => {
        if(loading&&loading.isShow){
            Toast.clear()
        }
        reject(err);
        let message = '请求失败！请检查网络';
        //错误返回
        if(err.response)message=err.response.message;
         Toast(message)
      })
  })
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

  post(url,data = {}){
  return new Promise((resolve,reject) => {
    url=baseUrl+url
    axios.post(url,data,{
      headers: {
        "token":localStorage.getItem("token")
      }
     })
      .then(response => {
        if(response.data.code === 200){
          resolve(response.data);
        }else{
          reject(response.data.message);
          Toast(response.data.message)
        }
      },err => {
        reject(err);
        let message = '请求失败！请检查网络';
        if(err.response)message=err.response.data.message;
        Toast(message)
      })
  })
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */

  patch(url,data = {}){
  return new Promise((resolve,reject) => {
    url=baseUrl+url
    axios.patch(url,data,{
      headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
        "token":localStorage.getItem("token")
      }
     })
      .then(response => {
        if(response.data.code === 200){
          resolve(response.data);
        }else{
          Toast(response.data.msg)
        }
      },err => {
        reject(err);
        let message = '请求失败！请检查网络';
        if(err.response)message=err.response.data.message;
        Toast(message)
      })
  })
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

  put(url,data = {}){
  return new Promise((resolve,reject) => {
    url=baseUrl+url
    axios.put(url,data,{
      headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
        "token":localStorage.getItem("token")
      }
     })
      .then(response => {
        if(response.data.code === 200){
          resolve(response.data.data);
        }else{
          Toast(response.data.msg)
        }
      },err => {
        reject(err);
        let message = '请求失败！请检查网络';
        if(err.response)message=err.response.message;
        Toast(message)
      })
  })
}

  del(url,data = {}){
  return new Promise((resolve,reject) => {
    axios.delete(url,data,{
      headers: {
       'Content-Type': 'application/x-www-form-urlencoded',
        "token":localStorage.getItem("token")
      }
     })
      .then(response => {
        if(response.data.code === 200){
          resolve(response.data.data);
        }else{
          Toast(response.data.msg)
        }
      },err => {
        reject(err);
        let message = '请求失败！请检查网络';
        if(err.response)message=err.response.message;
        Toast(message)
      })
  })
 }

}